package Test1;

import java.text.DecimalFormat;

/**
 * 表达式类，表示一个算术表达式
 * 支持加减乘除四则运算
 */
public class Expression {
    private int num1;
    private int num2;
    private Integer num3;
    private String op1;
    private String op2;
    private double result;
    private Double intermediateResult;
    private static final DecimalFormat df = new DecimalFormat("#.##");

    public Expression(int num1, String op1, int num2) {
        this.num1 = num1;
        this.op1 = op1;
        this.num2 = num2;
        this.num3 = null;
        this.op2 = null;
        calculateResult();
    }

    public Expression(int num1, String op1, int num2, String op2, int num3) {
        this.num1 = num1;
        this.op1 = op1;
        this.num2 = num2;
        this.op2 = op2;
        this.num3 = num3;
        calculateResult();
    }

    private void calculateResult() {
        if (num3 == null) {
            // 两个数的表达式
            result = calculateTwoNumbers(num1, op1, num2);
        } else {
            // 三个数的表达式，先计算中间结果
            intermediateResult = calculateTwoNumbers(num1, op1, num2);
            // 计算最终结果
            result = calculateTwoNumbers(intermediateResult, op2, num3);
        }
    }

    private double calculateTwoNumbers(double a, String op, double b) {
        switch (op) {
            case "+":
                return a + b;
            case "-":
                return a - b;
            case "×":
            case "*":
                return a * b;
            case "÷":
            case "/":
                if (b == 0) {
                    throw new ArithmeticException("除数不能为零");
                }
                return a / b;
            default:
                throw new IllegalArgumentException("未知的操作符: " + op);
        }
    }

    // Getter 方法
    public int getNum1() { return num1; }
    public int getNum2() { return num2; }
    public Integer getNum3() { return num3; }
    public String getOp1() { return op1; }
    public String getOp2() { return op2; }
    public double getResult() { return result; }
    public Double getIntermediateResult() { return intermediateResult; }

    /**
     * 检查结果是否为整数
     */
    public boolean isIntegerResult() {
        return result == (int) result;
    }

    /**
     * 获取整数结果（如果是整数）
     */
    public int getIntegerResult() {
        if (!isIntegerResult()) {
            throw new IllegalStateException("结果不是整数");
        }
        return (int) result;
    }

    @Override
    public String toString() {
        if (num3 == null) {
            return num1 + " " + op1 + " " + num2 + " = ";
        } else {
            return num1 + " " + op1 + " " + num2 + " " + op2 + " " + num3 + " = ";
        }
    }

    public String toStringWithAnswer() {
        String answer;
        if (isIntegerResult()) {
            answer = String.valueOf(getIntegerResult());
        } else {
            answer = df.format(result);
        }

        if (num3 == null) {
            return num1 + " " + op1 + " " + num2 + " = " + answer;
        } else {
            String intermediate = intermediateResult == (int) intermediateResult.doubleValue() ?
                    String.valueOf(intermediateResult.intValue()) : df.format(intermediateResult);
            return num1 + " " + op1 + " " + num2 + " " + op2 + " " + num3 + " = " + answer +
                    " (中间结果: " + intermediate + ")";
        }
    }

    /**
     * 转换为CSV格式
     */
    public String toCSV(boolean withAnswer) {
        if (withAnswer) {
            String answer = isIntegerResult() ? String.valueOf(getIntegerResult()) : df.format(result);
            return "\"" + toString().replace(" = ", "") + "\",\"" + answer + "\"";
        } else {
            return "\"" + toString().replace(" = ", "") + "\"";
        }
    }
}